﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Senparc.CO2NET.Extensions;
using Senparc.Weixin;
using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Menu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Senparc.CO2NET.HttpUtility;
using Senparc.NeuChar;
using Senparc.Weixin.CommonAPIs;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MP.Containers;
using Microsoft.AspNetCore.Authorization;

namespace iemtm.WeChat.Web.Entry.Areas.WeChat.Controllers
{
    [AllowAnonymous]
    public class MenuController : Controller
    {
        public static readonly string EncodingAESKey = "57eed0b88fb494940d29013a1e80628c";//与微信公众账号后台的EncodingAESKey设置保持一致，区分大小写。
        public static readonly string AppId = "wxe751c3acd8c3312e";//与微信公众账号后台的AppId设置保持一致，区分大小写。
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateMenuFromJson(string token, string fullJson)
        {
            //TODO:根据"conditionalmenu"判断自定义菜单

            var apiName = "使用JSON更新";
            try
            {
                //GetMenuResultFull resultFull = Newtonsoft.Json.JsonConvert.DeserializeObject<GetMenuResultFull>(fullJson);
                //初始化
                GetMenuResultFull resultFull = new GetMenuResultFull();
                MenuFull_ButtonGroup menu = new MenuFull_ButtonGroup();
                menu.button = new List<MenuFull_RootButton>();
                menu.button.Add(new MenuFull_RootButton() { key = "link", name = "链接", type = "view", url = "http://www.baidu.com" });
                menu.button.Add(new MenuFull_RootButton() { key = "OneClick", name = "图文链接", type = "click" });
                var subButton = new MenuFull_RootButton()
                {
                    key = "OneClick",
                    name = "二级菜单",
                    type = "click",
                };
                subButton.sub_button = new List<MenuFull_RootButton>();
                subButton.sub_button.Add(new MenuFull_RootButton()
                {
                    key = "SubClickRoot_Text",
                    name = "文字信息",
                    type = "click",
                });
                subButton.sub_button.Add(new MenuFull_RootButton()
                {
                    key = "SubClickRoot_Text",
                    name = "文字信息2",
                    type = "click",
                });
                menu.button.Add(subButton);
                //重新整理按钮信息
                WxJsonResult result = null;
                IButtonGroupBase buttonGroup = null;

                buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                result = CommonApi.CreateMenu(token, buttonGroup);

                var json = new
                {
                    Success = result.errmsg == "ok",
                    Message = "菜单更新成功。" + apiName
                };
                return Json(json, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() });
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = string.Format("更新失败：{0}。{1}", ex.Message, apiName) };
                return Json(json, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() });
            }
        }

        [HttpPost]
        public async Task<ActionResult> CreateMenuInit()
        {
         
        
  

            var apiName = "使用JSON更新";
            try
            {
                
                //初始化
                GetMenuResultFull resultFull = new GetMenuResultFull();
                MenuFull_ButtonGroup menu = new MenuFull_ButtonGroup();
                menu.button = new List<MenuFull_RootButton>();
                // menu.button.Add(new MenuFull_RootButton() { key = "link", name = "链接", type = "view", url = "http://www.baidu.com" });
                //  menu.button.Add(new MenuFull_RootButton() { key = "OneClick", name = "图文链接", type = "click" });
                string urlpreffix = "http://tcwxopen.natappvip.cc";

                #region 故障信息
                var subButton = new MenuFull_RootButton()
                {
                    key = "OneClick",
                    name = "故障信息",
                    type = "click",
                };
                subButton.sub_button = new List<MenuFull_RootButton>();
                subButton.sub_button.Add(new MenuFull_RootButton()
                {
                    key = "TimerPush",
                    name = "故障查询",
                    type = "click",
                   
                });
                subButton.sub_button.Add(new MenuFull_RootButton()
                {
                    name = "故障列表",
                    type = "view",
                    url = urlpreffix + "/Alarm/Index",
                });
                menu.button.Add(subButton);
                #endregion

                #region 发电管理
                var subButton1 = new MenuFull_RootButton()
                {
                    key = "OneClick",
                    name = "发电管理",
                    type = "click",
                };
                subButton1.sub_button = new List<MenuFull_RootButton>();
                subButton1.sub_button.Add(new MenuFull_RootButton()
                {
                  
                    name = "工单管理",
                    type = "view",
                    url = urlpreffix + "/AnnouceList/Index",
                });
                subButton1.sub_button.Add(new MenuFull_RootButton()
                {
                    name = "油机管理",
                    type = "view",
                    url = urlpreffix + "/Generator/Index",
                });
                menu.button.Add(subButton1);
                #endregion
                #region 运维管理
                var subButton2 = new MenuFull_RootButton()
                {
                    key = "OneClick",
                    name = "运维管理",
                    type = "click",
                };
                subButton2.sub_button = new List<MenuFull_RootButton>();
                subButton2.sub_button.Add(new MenuFull_RootButton()
                {

                    name = "运维百科",
                    type = "view",
                    url = urlpreffix + "/OpsInfo",
                });
                subButton2.sub_button.Add(new MenuFull_RootButton()
                {
                    name = "在线考试",
                    type = "view",
                    url = urlpreffix + "http://sfmes.rjtx.net:55291/MobileApp/index.html",
                });
                menu.button.Add(subButton2);
                #endregion


                var T = await iemtm.WeChat.Core.Extension.Helper.WeChatHelper.CreateMenu(menu);

                var json = new
                {
                    Success = "ok",
                    Message = "菜单更新成功。" + apiName
                };
                return Json(json, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() });
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = string.Format("更新失败：{0}。{1}", ex.Message, apiName) };
                return Json(json, new JsonSerializerSettings() { ContractResolver = new DefaultContractResolver() });
            }
        }
      
    }
}
